// 
//  common.js
//  tdaye
//  
//  Created by WillLee on 2011-07-09.
//  Copyright 2011 lishuzu. All rights reserved.
// 

var userAgent = navigator.userAgent.toLowerCase();
var is_opera = userAgent.indexOf('opera') != -1 && opera.version();
var is_moz = (navigator.product == 'Gecko') && userAgent.substr(userAgent.indexOf('firefox') + 8, 3);
var is_ie = (userAgent.indexOf('msie') != -1 && !is_opera) && userAgent.substr(userAgent.indexOf('msie') + 5, 3);
var is_safari = (userAgent.indexOf('webkit') != -1 || userAgent.indexOf('safari') != -1);
var note_step = 0;
var note_oldtitle = document.title;
var note_timer;
//删除消息
function delPm(touid, pmid){
	$.ajax({
		type: 'POST',
		url: baseurl+'ajax/del_pm',
		data: {
			param:{
				pmid:pmid,
				touid:touid
			}
		},
	  success: function(result){
				var jsonstr = $.toJSON(result);
				obj = $.parseJSON(jsonstr); 
			if (obj.code != '1') {
				alert(obj.msg);
			}else{
				var delpmbox = $('#pm_'+pmid);
				delpmbox.fadeOut('300',function(){
					delpmbox.fadeIn('300',function() {
						delpmbox.fadeOut('slow');
					});
				});

			};
		},
	  dataType: 'json'
	});
}
// 检查表单内容长度
function checklen(obj, length){
	text = $(obj).val().replace(/[^\x00-\xff]/g, "**");
	if(text.length>=length){
		return length-text.length;
	}else{
		return text.length;
	}
}

// 中文转码
function cnCode(str) {
	return is_ie && document.charset == 'utf-8' ? encodeURIComponent(str) : str;
}


function toggleComment(itemId, idType){
	var fadespeed = 600;
	var common_detal = $('#comment_'+idType+'_'+itemId);
	
	if (common_detal.css('display') == 'none') {
		getComment(itemId, idType);
		common_detal.slideDown(fadespeed);
	}else{
		common_detal.slideUp(fadespeed);	
	};

}

function getComment(itemId, idType){
	$.ajax({
	  type: 'POST',
	  url: baseurl+'ajax/get_comments',
	  data: {
			param:{
				id:itemId,
				idtype:idType
			},
			key:'47edbd4fcb4e0ba3c240949542545828'
			},
	  success: function(result){
			
			$('#comment_'+idType+'_'+itemId).html(result);
			$('#comment_'+idType+'_'+itemId).slideDown('600');
		},
	  dataType: 'html'
	});
}

function sendComment(itemId, idType){
	if ($('#message_'+idType+'_'+itemId).val().length <=0) {
		alert('您还没有填写评论');
		return false;
	};
	if ($('#message_'+idType+'_'+itemId).val().length >200) {
		alert($('#message_'+idType+'_'+itemId).val().length);
		return false;
	};
	$.ajax({
		type: 'POST',
		url: baseurl+'ajax/add_comment',
		data: {
			param:{
				id:itemId,
				idtype:idType,
				message:$('#message_'+idType+'_'+itemId).val()
			},
			key:$('#comment_key_'+idType+'_'+itemId).val()
		},
	  success: function(result){
				var jsonstr = $.toJSON(result);
				obj = $.parseJSON(jsonstr); 
			if (obj.code != '1') {
				alert(obj.msg);
			};
			getComment(itemId, idType);
		},
	  dataType: 'json'
	});
}

function delComment(cid, itemId, idType){
	$.ajax({
		type: 'POST',
		url: baseurl+'ajax/del_comment',
		data: {
			param:{
				cid:cid,
				id:itemId,
				idtype:idType
			},
			key:$('#comment_key_'+idType+'_'+itemId).val()
		},
	  success: function(result){
				var jsonstr = $.toJSON(result);
				obj = $.parseJSON(jsonstr); 
			if (obj.code != '1') {
				alert(obj.msg);
			}else{
				$('#comment_'+cid+'_li').hide('normal');
			};
			getComment(itemId, idType);
		},
	  dataType: 'json'
	});
}

function islike(Itemid){
	if ($('#like_icon_'+Itemid).attr('src') == baseurl+'resources/images/likeon.gif') {
		unlike(Itemid);
	}else{
		like(Itemid);		
	}
}
function like(Itemid){
	$.ajax({
		type: 'POST',
		url: baseurl+'ajax/like',
		data: {
			param:{
				wid:Itemid
			},
			key:$('#work_key_'+Itemid).val()
		},
	  success: function(result){
				var jsonstr = $.toJSON(result);
				obj = $.parseJSON(jsonstr); 
			if (obj.code == '1') {
				$('#like_icon_'+Itemid).attr('src', baseurl+'resources/images/likeon.gif');
				var likenum = parseInt($('#likenum_'+Itemid).text())+1;
				$('#likenum_'+Itemid).text(likenum);
			}else if(obj.code == '2'){
				alert('登陆之后才能进行喜欢操作哦~');
				// $('#ajax_login').dialog('open');
			}else{
				alert(obj.msg);
			};
		},
	  dataType: 'json'
	});	
}

function unlike(Itemid){
	$.ajax({
		type: 'POST',
		url: baseurl+'ajax/unlike',
		data: {
			param:{
				wid:Itemid
			},
			key:$('#work_key_'+Itemid).val()
		},
		
	  success: function(result){
				var jsonstr = $.toJSON(result);
				obj = $.parseJSON(jsonstr); 
			if (obj.code != '1') {
				alert(obj.msg);
			}else{
				$('#like_icon_'+Itemid).attr('src', baseurl+'resources/images/likeoff.gif');
				var likenum = parseInt($('#likenum_'+Itemid).text())-1;
				$('#likenum_'+Itemid).text(likenum);
			};
		},
	  dataType: 'json'
	});	
}

function isfollow(uid){
	if ($('#shipcode')) {
		if ($('#shipcode').val() == 2 || $('#shipcode').val() ==4) {
			follow(uid);
		}else if ($('#shipcode').val() == 1 || $('#shipcode').val() ==3){
			unfollow(uid);
		}else{
			alert('请登陆');
		}
	};
}
// 关注
function follow(Itemid){
	$.ajax({
		type: 'POST',
		url: baseurl+'ajax/follow',
		data: {
			param:{
				uid:Itemid
			},
			key:''
		},
		
	  success: function(result){
				var jsonstr = $.toJSON(result);
				obj = $.parseJSON(jsonstr); 
			if (obj.code != '1') {
				alert(obj.msg);
			}else{
				$('#followbutton').text('已关注,取消');
				$('#shipcode').val(1);
			};
		},
	  dataType: 'json'
	});	
}

// 取消关注
function unfollow(Itemid){
	$.ajax({
		type: 'POST',
		url: baseurl+'ajax/unfollow',
		data: {
			param:{
				uid:Itemid
			},
			key:''
		},
		
	  success: function(result){
				var jsonstr = $.toJSON(result);
				obj = $.parseJSON(jsonstr); 
			if (obj.code != '1') {
				alert(obj.msg);
			}else{
				$('#followbutton').text('加关注');
				$('#shipcode').val(2);
			};
		},
	  dataType: 'json'
	});	
}


// 分享函数
function shareTo(shareId, url, title, pic){
	switch (shareId){
	   case 'sina':
			var param = {
				url: url,
				type:'3',
				count:'0', /**是否显示分享数，1显示(可选)*/
				appkey:'35258811', /**您申请的应用appkey,显示分享来源(可选)*/
				title:title, /**分享的文字内容(可选，默认为所在页面的title)*/
				pic:pic, /**分享图片的路径(可选)*/
				ralateUid:'', /**关联用户的UID，分享微博会@该用户(可选)*/
				rnd:new Date().valueOf()
			}
			var api = 'http://service.weibo.com/share/share.php?';
	     break
	   case 'qq':
		     var param = {
				title:title,
				url:encodeURIComponent(url),
				appkey:encodeURI(""),
				pic:encodeURI(pic),
				site:'http://www.tdaye.com'
			}
			var api = 'http://v.t.qq.com/share/share.php?';
	     break
	   case 'qzone':
			var param = {
				url:url,
				desc:'',/*默认分享理由(可选)*/
				summary:'',/*摘要(可选)*/
				title:title,/*分享标题(可选)*/
				site:'http://www.6block.com',/*分享来源 如：腾讯网(可选)*/
				pics:pic /*分享图片的路径(可选)*/
			}
			var api = 'http://sns.qzone.qq.com/cgi-bin/qzshare/cgi_qzshare_onekey?';
	     break
	   case 'kaixin001':
			var param = {
				rtitle:title,
				rcontent:title,/*默认分享理由(可选)*/
				rurl:url/*摘要(可选)*/
			}
			var api = 'http://www.kaixin001.com/repaste/bshare.php?';
	     break
	   case 'douban':
			var param = {
				url:url,
				title:title,/*默认分享理由(可选)*/
				sel:encodeURIComponent(title),
				v:1
			}
			var api = 'http://www.douban.com/recommend/?';
	     break
	   default:
		var param = {}
		var api = '';
	}
	if (param && api) {
		var urlparam = [];
		for( var p in param ){
			urlparam.push(p + '=' + encodeURIComponent( param[p] || '' ) )
		}
		var shareapi = api+ urlparam.join('&');
		window.open( shareapi,'', 'width=700, height=680, top=0, left=0, toolbar=no, menubar=no, scrollbars=no, location=yes, resizable=no, status=no' );		
	};
}

//获取关键字
function relatekw() {
	var subject = cnCode($('#title').val());
	var message = cnCode($('#description').val());
	if(message) {
		message = message.substr(0, 500);
		message = message.replace(/&/ig, '', message);
	}
	
	$.get(baseurl+'relate/index/?subjectenc=' + subject + '&messageenc=' + message, function(s){
		obj = $.parseJSON(s);
		if (obj.code ==1) {
			$('#tag').val(obj.msg);
		};

	});
}
